using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class LoaiBenhDAO
    {
        DataProvider Provider;
        public LoaiBenhDAO()
        {
            Provider = new DataProvider();
        }

        public DataTable LayMaLoaiBenh()
        {
            Provider.Connect();
            string str = "Select MaLoaiBenh From LoaiBenh Where MaLoaiBenh >= all (Select MaLoaiBenh From LoaiBenh)";
            DataTable dt = new DataTable();
            OleDbDataAdapter da;
            da = new OleDbDataAdapter(str, Provider.cn);
            da.Fill(dt);
            Provider.Disconnect();
            return dt;
        }

        public DataTable LayBangLoaiBenh()
        {
            Provider.Connect();
            string str = "select * from LoaiBenh";
            DataTable dt = new DataTable();
            OleDbDataAdapter da;
            da = new OleDbDataAdapter(str, Provider.cn);
            da.Fill(dt);
            Provider.Disconnect();
            return dt;
        }


        public void ThemLoaiBenh(LoaiBenhDTO lbDto)
        {
            Provider.Connect();
            string str;
            str = "Insert into LoaiBenh(TenLoaiBenh, MaLoaiBenh) values (?, ?)";

            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);
            cmd.Parameters.Add("@TenLoaiBenh", OleDbType.WChar);
            cmd.Parameters.Add("@MaLoaiBenh", OleDbType.WChar);

            cmd.Parameters["@TenLoaiBenh"].Value = lbDto.TenLoaiBenh;
            cmd.Parameters["@MaLoaiBenh"].Value = lbDto.MaLoaiBenh;

            cmd.ExecuteNonQuery();
            Provider.Disconnect();

        }


        public void XoaLoaiBenh(string MaLoaiBenh)
        {
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            Provider.Connect();
            // B3: Tao chuoi strSQL thao tac CSDL
            string str;
            str = "Delete From LoaiBenh Where MaLoaiBenh = ?";
            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);
            cmd.Parameters.Add("@MaLoaiBenh", OleDbType.Char);
            cmd.Parameters["@MaLoaiBenh"].Value = MaLoaiBenh;
            cmd.ExecuteNonQuery();
            // B5: Dong ket noi CSDL
            Provider.Disconnect();
        }

        public void SuaLoaiBenh(LoaiBenhDTO lbDto)
        {
            Provider.Connect();
            // B3: Tao chuoi strSQL thao tac CSDL
            string str;
            str = "Update LoaiBenh Set TenLoaiBenh = ? Where MaLoaiBenh = ? ";
            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);

            cmd.Parameters.Add("@TenLoaiBenh", OleDbType.WChar);
            cmd.Parameters.Add("@MaLoaiBenh", OleDbType.WChar);

            cmd.Parameters["@TenLoaiBenh"].Value = lbDto.TenLoaiBenh;
            cmd.Parameters["@MaLoaiBenh"].Value = lbDto.MaLoaiBenh;

            cmd.ExecuteNonQuery();
            Provider.Disconnect();
        }


        public DataTable TimKiemTheoMaLoaiBenh(string MaLoaiBenh)
        {
            Provider.Connect();
            DataTable dt = new DataTable();
            //B3: Tao chuoi strSQL thao tac CSDL
            string str = "Select * From LoaiBenh Where MaLoaiBenh = " + "'" + MaLoaiBenh + "'";
            //B4: Thuc thi chuoi strSQL
            OleDbCommand cmd = new OleDbCommand(str, Provider.cn);

            cmd.Parameters.Add("@MaLoaiBenh", OleDbType.WChar);
            cmd.Parameters["@MaLoaiBenh"].Value = MaLoaiBenh;

            OleDbDataAdapter da;
            da = new OleDbDataAdapter(str, Provider.cn);
            da.Fill(dt);
            Provider.Disconnect();
            return dt;
        }
    }
}
